package com.leizi.igym.manager;

import com.leizi.igym.config.CosClientConfig;
import com.leizi.igym.properties.COSProperties;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-04  10:55
 * @Description: Cos 对象存储操作
 */

@Component
public class CosManager {
    @Resource
    private COSClient cosClient;

    @Resource
    private COSProperties cosProperties;
    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param localFilePath 本地文件路径
     * @return
     */
    public PutObjectResult putObject(String key, String localFilePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosProperties.getBucketName(), key,
                new File(localFilePath));
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param file 文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosProperties.getBucketName(), key,
                file);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     *  删除文件
     * @param key
     * @return: void
     **/
    public void deleteObject(String key){
        cosClient.deleteObject(cosProperties.getBucketName(),key);
    }
}
